<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Stadium;
use App\Detail;
use App\Order;
use Auth;
use App\Cat;

class StadiumController extends Controller
{

  public function getStadium($cid=1)
  {
    //test


    //运动项目
    $cats = Cat::where('pid',0)->get();
    //区域
    $areas = Cat::where('pid',$cid)->get();

    //判断区域数据,获取场馆数据
    if (!$areas->isEmpty() ) {

        $areas_id = [];
        foreach($areas as $v){
            array_push($areas_id, $v->cat_id);
        }
        $stadiums = $this->convertImg(Stadium::whereIn('cat_id',$areas_id)->get());
        // dd($stadiums);
    }else{
        //按照get的$cid重新获取
        $areas = Cat::where('cat_id',$cid)->get();

        //如果没有该项目就跳转
        if ($areas->isEmpty()) {
            return redirect('/stadium/1');
        }


        $areas_id[] = $areas->toArray()[0]['cat_id'];

        $stadiums = $this->convertImg(Stadium::whereIn('cat_id',$areas_id)->get());
    }
    return view('stadium_list',['cats'=>$cats,'areas'=>$areas,'stadiums'=>$stadiums]);

  }



    public function convertImg($collection)
    {
        foreach ($collection as  $value) {
            $value->images = explode(',', $value->images);
        }
        return $collection;
    }



  public function stadiumInfo()
  {
    return view('stadium_info');
  }

  //获取场馆详细信息

  public function detail($id = 1, $d = null){
      $today = date('Y-m-d', time());

      if (!$d) {
          $d = $today;
      } elseif ($d < $today) {
          return redirect('/stadium_detail/1');
      }

      $stadium = Stadium::where('stadium_id', $id)->get();

      $images = $this->convertImg($stadium);
      $stadium = $stadium->toArray();
      //dd($stadium);
      $detail = new Detail;
      //$data = $detail->where('stadium_id', $id)->get();
      $date = $detail->select('date', 'stadium_id')->where('date','>=', $today)->where('stadium_id', $id)->groupBy('date')->get();
      $number = $detail->select('number')->where('date', $d)->where('stadium_id', $id)->groupBy('number')->get();
      $time = $detail->select('time')->where('date', $d)->where('stadium_id', $id)->where('number', 1)->get();
      //dd($time);
      $price = $detail->where('date', $d)->where('stadium_id', $id)->get();
      //dd($price);
      return view('stadium_info', ['date'=>$date, 'number'=>$number, 'time'=>$time, 'price'=>$price, 'd'=>$d, 'stadium'=>$stadium]);

  }

  //提交场馆订单信息
  public function postorder(Request $req){
      if (!Auth::user()) {
          return redirect('/auth/login');
      }
      $order_sn = date('Ymd', time()).mt_rand(1000, 9999);
      $details = new Detail();
      $order = new Order();
      $data = $req->space_value;
      foreach($data as $d){
          $arr = explode('_',$d);
          $detail_one = $details->where('date', $arr[4])->where('number', $arr[0])->where('time', $arr[1].':00')->first();
          //dd($detail_one);
          $detail_one->status = 0;
          $detail_one->save();
          // $order->order_sn = $order_sn;
          // $order->user_id = $req->user()->user_id;
          // $order->stadium_id = 1;
          // $order->stadium_name = 'aaa';
          // $order->usetime = $arr[4];
          // $order->number = $arr[0];
          // $order->status = 0;
          // $order->money = $arr[3];
          // $order->pubtime = time();
          // $order->record = 0;
          // $order->save();
          $order_data = ['order_sn'=>$order_sn,
                          'user_id' => $req->user()->user_id,
                          'stadium_id' => 1,
                          'stadium_name' => 'aaa',
                          'usetime' => $arr[4], //日期
                          'number' => $arr[0],  //几号场
                          'status' => 0,
                          'money' => $arr[3],  //场馆预订价格
                          'pubtime' => $arr[1], //场馆时间
                          'record' => 0,
                      ];
          $order->create($order_data);
      }
      return redirect('/pay');
  }
}

